var App = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:1,// 当前页码
    last_page:1,
    scrollHeight: null,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    //头部数据统计
    site_count:[], 
    
    // 门店下拉
    storeList:[],
    storeindex:0,  //下标
    store_name:'', //选中回显
    store_id:'',    //选中的门店ID
    // 工位状态下拉
    statusList:[],
    statusindex:0,  //下标
    status_name:'', //选中回显
    status:0,    //选中的门店ID
     // 删除记录+强制结算提示语
     delTips :'',
     settleTips :'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      isLogin: App.checkIsLogin(),
      page:1,// 当前页码
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    });
    console.log('login',that.data.isLogin)
     //设置页面高度
     this.setListHeight();
     // 获取门店列表
     this.getStore()
     // 获取工位状态列表
     this.getStatus()
     // 获取工位数据列表
     that.getDataList();
  },
  
  // 获取门店列表
  getStore(){
    let that = this;
    let obj = {b_id:App.get_b_id()}
    App.request('/Manage/Management/storeList',obj,function(e){
      console.log(e);
        if (e.code == "200") {
          that.setData({
            storeList:e.result,
          })
        } else {
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000
          })
        }
    })
  },
  // 工位状态
  getStatus(){
    let that = this;
    let obj = {}
    App.request('/Manage/Management/siteStatus',obj,function(e){
        if (e.code == "200") {
          that.setData({statusList:e.result})
        } else {
          wx.showToast({title: e.message,icon: 'none',duration: 2000})// 错误
        }
    })
  },
  //获取数据列表
  getDataList(isPage, page){
    let that = this;
    let obj = {
      b_id:App.get_b_id(),
      store_id:this.data.store_id,
      site_status:this.data.status
    }
    App.request('/Manage/Management/siteManage',obj,function(e){
      console.log(e);
        // 不加这个方法真机下拉会一直处于刷新状态，无法复位
        wx.stopPullDownRefresh()
        if (e.code == "200") {
          that.setData({
            site_count:e.result.site_count,
            list:e.result.sites,
            delTips :e.result.tips.delTips,
            settleTips :e.result.tips.settleTips,
          })
        } else {
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000
          })
        }
    })
  },
  
  //选择店铺
  bindStorePicker: function (e) {
    this.setData({
      storeindex: e.detail.value,
      store_id:this.data.storeList[e.detail.value].store_id,
      store_name:this.data.storeList[e.detail.value].title,
    })
    this.getDataList() //获取数据列表
  },
  // 选择工位状态
  bindStatusPicker: function (e) {
    this.setData({
      statusindex: e.detail.value,
      status:this.data.statusList[e.detail.value].value,
      status_name:this.data.statusList[e.detail.value].name,
    })
    this.getDataList()//获取数据列表
  },
  // 开状态按钮/供电状态修改
  switchChange:function(e){
    let that = this
    let obj={
      io:e.currentTarget.dataset.type,
      lot_id:e.currentTarget.dataset.id,
      status:e.detail.value==true?0:1
    }
    console.log('obj',obj)
    App.request('/Manage/Management/lotSwitch',obj,function(res){
        if (res.code == "200") {
          wx.showToast({
            title:res.msg,
            icon: 'success',
            duration: 2000
          })    
          // that.getDataList() 
        } 
    })
  },
  // 开门/关门
  openDoor:function(e){
    let that = this
    let obj={
      io:e.currentTarget.dataset.type,
      lot_id:e.currentTarget.dataset.id,
      have_lot : e.currentTarget.dataset.lot,
      in_cam_id:e.currentTarget.dataset.cam,
    }
    console.log('obj',obj)
    App.request('/Manage/Management/doorManage',obj,function(res){
        if (res.code == "200") {
          wx.showToast({
            title:res.msg,
            icon: 'success',
            duration: 2000
          })    
          that.getDataList() 
        } 
    })
  },
// 删除记录
delete: function (e){
  let that = this;
  let obj = {order_id:e.currentTarget.dataset.id}
  wx.showModal({
    title: "提示",
    content: that.data.delTips,
    success(e) {
      if(e.confirm == true){
        App.request('/Manage/Management/washingDel',obj,function(e){
          if (e.code == "200") {
            that.getDataList()//获取数据列表
            App.showSuccessToast(e.msg);
          } 
        })
      }
    }
  });
  
},
// 强制结算
leave: function (e){
  let that = this;
  let obj = {site_id:e.currentTarget.dataset.id}
  wx.showModal({
    title: "提示",
    content: that.data.settleTips,
    success(e) {
      if(e.confirm == true){
        App.request('/Manage/Management/forceSettle',obj,function(e){
          if (e.code == "200") {
            that.getDataList()//获取数据列表
            App.showSuccessToast(e.msg);
          } 
        })
      }
    }
  });
  
},
// 跳转到会员明细
to_info(e){
  wx.navigateTo({
    url: '/pages/member/info?cid='+e.currentTarget.dataset.id,
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 获取工位数据列表
    this.getDataList();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  /**
   * 设置文章列表高度
   */
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 78), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
      console.log(systemInfo.windowHeight,scrollHeight);
      this.setData({
        scrollHeight
      });
  },
})